package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.*;
import com.dqyt.app.mongo.OtherInputDao;
import com.dqyt.app.mongo.ReportDao;
import com.dqyt.app.mongo.SampleDao;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Constant;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.config.Config;
import com.dqyt.khjc.mapper.DownFileMapper;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.mapper.SealMapper;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.dqyt.quart.AddSimpleJobReq;
import com.dqyt.quart.DeleteJobReq;
import com.dqyt.quart.QuartzManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

@Service
public class ReportPcService {


    @Autowired
    private ReportDao reportDao;

    @Autowired
    private SampleDao mSampleDao;

    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private SealMapper sealMapper;

    @Autowired
    private DownFileMapper downFileMapper;



    @Autowired
    private QuartzManager quartzManager;

    @Autowired
    private OtherInputDao otherInputDao;

    /**
     * 分页查询 我的报告
     *
     * @param pageNum      页数
     * @param pageSize     每页条数
     * @param bsbh         标识编号
     * @param
     * @param type         样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 设备状态   1：已接受   2：检测中 3：检测不合格 4：合格 5：维修中 6:已废弃
     * @param wtfId        委托方id
     * @param spType       送检类型
     * @return
     */
    public ResultMsg getReportListPage(HttpServletRequest request, Integer pageNum, Integer pageSize, String bsbh,
                                       String merweima, int type, int reportStatue, int wtfId, int spType,
                                       int zzcsId, int reportProgress, String jyrqNian, String jyrqYue,String jyyId,String rwdbh,String sbbh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_QBBG)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }

            //将含有这个的替换 没有就不替换
            String erweima = merweima.replace("DQYT：", "DQYT:");

            //long  ctime = TimeUtil.currentTimeMillis();
            //分页查询数据
            List<Report> samples = reportDao.getReportListPage(pageNum, pageSize, bsbh, erweima, type, reportStatue, wtfId, spType, 1, zzcsId, jyyId, reportProgress, jyrqNian, jyrqYue,rwdbh,sbbh);
            //long  ctime2 = TimeUtil.currentTimeMillis();
            //long aaaa = ctime2 - ctime;
            //System.out.println("zzzzzzzzzz查询总数据的时间=="+aaaa);

            //long  ctime3 = TimeUtil.currentTimeMillis();
            //查询总条数
            long total = reportDao.getReportListTotle(bsbh, erweima, type, reportStatue, wtfId, spType, 1, zzcsId, jyyId, reportProgress, jyrqNian, jyrqYue,rwdbh,sbbh);
            //long  ctime4 = TimeUtil.currentTimeMillis();
            //long bbbb = ctime2 - ctime;
            //System.out.println("zzzzzzzzzz查询总条数的时间数据的时间=="+bbbb);

            RespPageBean bean = new RespPageBean();
            bean.setData(samples);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;


    }



    /**
     * 分页查询 我的报告
     *
     * @param pageNum      页数
     * @param pageSize     每页条数
     * @param bsbh         标识编号
     * @param
     * @param type         样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 设备状态   1：已接受   2：检测中 3：检测不合格 4：合格 5：维修中 6:已废弃
     * @param wtfId        委托方id
     * @param spType       送检类型
     * @return
     */
    public ResultMsg getReportListPage2(HttpServletRequest request, Integer pageNum, Integer pageSize, String bsbh,
                                        String merweima, int type, int reportStatue, int wtfId, int spType,
                                        int zzcsId, int reportProgress, String jyrqNian, String jyrqYue,String jyyId,String rwdbh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_QBBG)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }

            //将含有这个的替换 没有就不替换
            String erweima = merweima.replace("DQYT：", "DQYT:");

            long  ctime = TimeUtil.currentTimeMillis();
            //分页查询数据
            List<Report> samples = reportDao.getReportListPage2(pageNum, pageSize, bsbh, erweima, type, reportStatue, wtfId, spType, 1, zzcsId, jyyId, reportProgress, jyrqNian, jyrqYue, rwdbh);
            long  ctime2 = TimeUtil.currentTimeMillis();
            long aaaa = ctime2 - ctime;


            long  ctime3 = TimeUtil.currentTimeMillis();
            //查询总条数
            long total = reportDao.getReportListTotle2(bsbh, erweima, type, reportStatue, wtfId, spType, 1, zzcsId, jyyId, reportProgress, jyrqNian, jyrqYue,rwdbh);
            long  ctime4 = TimeUtil.currentTimeMillis();
            long bbbb = ctime2 - ctime;
            RespPageBean bean = new RespPageBean();
            bean.setData(samples);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;


    }








    /**
     * 分页查询 我的报告
     *
     * @param pageNum      页数
     * @param pageSize     每页条数
     * @param bsbh         标识编号
     * @param
     * @param type         样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 设备状态   1：已接受   2：检测中 3：检测不合格 4：合格 5：维修中 6:已废弃
     * @param wtfId        委托方id
     * @param spType       送检类型
     * @return
     */
    public ResultMsg getMyReportListPage(HttpServletRequest request, Integer pageNum, Integer pageSize, String bsbh,
                                         String merweima, int type, int reportStatue, int wtfId, int wtfIdChi,int spType,
                                         int zzcsId, int reportProgress, String jyrqNian, String jyrqYue,String rwdbh,String sbbh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_QBBG)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }

            //将含有这个的替换 没有就不替换
            String erweima = merweima.replace("DQYT：", "DQYT:");

            String jyyId = String.valueOf(user.getUserId());

            //分页查询数据
            List<Report> samples = reportDao.getMyReportListPage(pageNum, pageSize, bsbh, erweima, type, reportStatue, wtfId,wtfIdChi, spType, 1, zzcsId, jyyId, reportProgress, jyrqNian, jyrqYue, rwdbh,sbbh);
            //查询总条数
            long total = reportDao.getMyReportListTotle(bsbh, erweima, type, reportStatue, wtfId,wtfIdChi, spType, 1, zzcsId, jyyId, reportProgress, jyrqNian, jyrqYue, rwdbh,sbbh);
            RespPageBean bean = new RespPageBean();
            bean.setData(samples);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;


    }


    /**
     * 成套查询报告
     *
     * @param pageNum      页数
     * @param pageSize     每页条数
     * @param bsbh         标识编号
     * @param
     * @param type         样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 设备状态   1：已接受   2：检测中 3：检测不合格 4：合格 5：维修中 6:已废弃
     * @param wtfId        委托方id
     * @param spType       送检类型
     * @return
     */
    public ResultMsg getMyReportTaoPage(HttpServletRequest request, Integer pageNum, Integer pageSize, String bsbh,
                                        String merweima, int type, int reportStatue, int wtfId, int spType, int zzcsId, int reportProgress, String jyrqNian, String jyrqYue) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_BGCX)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }
            List<Report> samples = new ArrayList<>();
            //先查询 面罩报告
            Report reportMz =  reportDao.getReportBybsbh(bsbh,1);
            if(null != reportMz){
                if(!"".equals(reportMz.respirator.qpbsh)){
                    List<Report>  reportList =  reportDao.getMoreMzByPar(reportMz.jyrqNian,reportMz.respirator.qpbsh,1,reportMz.bsbh);
                    if(reportList.size()>1){
                        //
                        String messageStr ="呼吸器设备编号，";
                        for(Report report:reportList){
                            messageStr+= ""+report.sbbh+ " ,";
                        }
                        messageStr+="的呼吸器同时匹配了相同的气瓶";
                        ret.setCode(ResultMsg.ERROR);
                        ret.setMsg(messageStr);
                        return ret;
                    }
                }
                samples.add(reportMz);
                //查询压力表 报告
                Report reportYlb =  reportDao.getReportBybsbh(bsbh,3);
                if(null != reportYlb){
                    samples.add(reportYlb);
                }
                //先看看有没有 匹配的气瓶信息
                if(null != reportMz.respirator.qpbsh && !"".equals(reportMz.respirator.qpbsh)){
                    //查询匹配的气瓶
                    Report reportQp =  reportDao.getReportBybsbh(reportMz.respirator.qpbsh,2);
                    if(null != reportQp){
                        samples.add(reportQp);
                    }
                }
            }
            RespPageBean bean = new RespPageBean();
            bean.setData(samples);
            bean.setTotal(0L);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;


    }




    /**
     * 设置复合气瓶的 报告状态合格
     *
     * @param request
     * @param bsbh
     * @return
     */
    public ResultMsg setFhqpHg(HttpServletRequest request,int type, String bh, String bsbh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_WDBG)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);
            if(type == 3){ //压力白哦
                report.pressure.jl="本次压力表校验合格";
            }
            //如果是面罩 直接填写外观合格
            if(report.type == Report.BG_TYPE_HXQ){
                report.respirator.nr_wg_jyjg = "符合要求";
                report.respirator.nr_wg_dxpd = "合格";
                report.respirator.jyjl="合格";
            }

            report.reportStatue = Constant.RRPORT_STATUE_HG;

            //设置子类型
            report.failedStart = 0;
            //清空原因
            report.bhgyy = "";
            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            reportDao.save(report);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("修改成功");

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "]将标识编号为： [" + report.bsbh + "]的报告的状态变更为：合格");
                historyBean.setType(Opt.BGGL_BGHG);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 报告不合格
     *
     * @param request
     * @param bh
     * @param bsbh
     * @param failedStart 子分类
     * @param jl          结论
     * @return
     */
    public ResultMsg reportFhqpFied(HttpServletRequest request, String bh, String bsbh, int failedStart, String jl) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
//            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DJYBG) && !CheckTool.checkAuthority(user, AuthorityID.BGGL_DPZBG)
//                    && !CheckTool.checkAuthority(user, AuthorityID.BGGL_DSHBG)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);
            //设备报告的状态为不合格
            report.reportStatue = Constant.RRPORT_STATUE_BHG;
            //设置不合格子分类
            report.failedStart = failedStart;

            //如果是 报废
            if (failedStart == 2) {
                report.cylinders.cljg = "报废";
            }
            //设备结论
            report.cylinders.jl = jl;
            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            //保存报告
            reportDao.save(report);

            //查询样品信息
            Sample sample = mSampleDao.getSampleById(bsbh);
            if (null != sample) {
                //设置样品状态为 不合格
                sample.sampleStatue = Sample.YP_STATUE_BGSHZ;
                //更新数据库
                mSampleDao.save(sample);
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("成功");

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "]将标识编号为： [" + report.bsbh + "]的报告的状态变更为：不合格");
                historyBean.setType(Opt.BGGL_BGBHE);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 报告不合格
     *
     * @param request
     * @param bh
     * @param bsbh
     * @param failedStart 子分类
     * @param jl          结论
     * @return
     */
    public ResultMsg reportFied(HttpServletRequest request, String bh, String bsbh, int failedStart, String jl,String nr_ypztms) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);
            //设备报告的状态为不合格
            report.reportStatue = Constant.RRPORT_STATUE_BHG;
            //设置不合格子分类
            report.failedStart = failedStart;

            //如果是压力表
            if (report.type == Report.BG_TYPE_YLB) {
                //设置结论
                report.pressure.jl = jl;
            } else if (report.type == Report.BG_TYPE_HXQ) {  //如果是呼吸器
                //设置结论
                report.respirator.nr_bz = jl;
                report.respirator.jyjl = "不合格";
                if (failedStart == 1) { //如果是外观不合格
                    report.respirator.nr_wg_jyjg = "不符合要求";
                    report.respirator.nr_wg_dxpd = "不合格";
                } else if (failedStart == 2) {//如果是监测项不合格
                    report.respirator.nr_wg_jyjg = "符合要求";
                    report.respirator.nr_wg_dxpd = "合格";
                } else {
                    report.respirator.nr_wg_jyjg = "符合要求";
                    report.respirator.nr_wg_dxpd = "合格";
                }
                //如果样品状态描述 不为空就填写上
                if(!"".equals(nr_ypztms)){
                    report.respirator.nr_ypztms =nr_ypztms;
                }
            }
            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            //保存报告
            reportDao.save(report);
            //查询样品信息
            Sample sample = mSampleDao.getSampleById(bsbh);
            if (null != sample) {
                //设置样品状态为 不合格
                sample.sampleStatue = Sample.YP_STATUE_BGSHZ;
                //更新数据库
                mSampleDao.save(sample);
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(report);
            ret.setMsg("成功");


            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "]将标识编号为： [" + report.bsbh + "]的报告的状态变更为：不合格");
                historyBean.setType(Opt.BGGL_BGBHE);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 修改报告的进度
     *
     * @param request
     * @param bh
     * @param bsbh
     * @param reportProgress
     * @return
     */
    public ResultMsg srtReportProgress(HttpServletRequest request, String bh, String bsbh, int reportProgress) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_WDBG)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);
            report.reportProgress = reportProgress;
            //清除理由
            report.bhgyy = "";
            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            reportDao.save(report);
            ret.setCode(ResultMsg.SUCESS);



            DeleteJobReq deleteJobReq = new DeleteJobReq();
            deleteJobReq.setJobName(Config.SHBG_TASK);
            deleteJobReq.setJobGroupName(Config.SHBG_TASK_GROUP);
            deleteJobReq.setTriggerName(Config.SHBG_TASK);
            deleteJobReq.setTriggerGroupName(Config.SHBG_TASK_GROUP);
            quartzManager.removeJob(deleteJobReq);



            Calendar beforeTime = Calendar.getInstance();
            // 5 秒之后的时间
            beforeTime.add(Calendar.SECOND, Config.MINUTE_15);
            Date beforeDate = beforeTime.getTime();
            AddSimpleJobReq addSimpleJobReq = new AddSimpleJobReq();
            addSimpleJobReq.setDate(beforeDate);
            addSimpleJobReq.setJobClass("ShrwTask");
            quartzManager.addSimpleJob(addSimpleJobReq, Config.SHBG_TASK,Config.SHBG_TASK_GROUP);


            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                if(reportProgress ==1){
                    historyBean.setMessage("[" + user.getUserName() + "] 操作后，标识编号为： [" + report.bsbh + "]的报告的进度变更为：待检验");
                }else if(reportProgress ==2){
                    historyBean.setMessage("[" + user.getUserName() + "] 操作后，标识编号为： [" + report.bsbh + "]的报告的进度变更为：待核验");
                }else if(reportProgress ==3){
                    historyBean.setMessage("[" + user.getUserName() + "]操作后，标识编号为： [" + report.bsbh + "]的报告的进度变更为：待批准");
                }else if(reportProgress ==4){
                    historyBean.setMessage("[" + user.getUserName() + "]操作后，标识编号为： [" + report.bsbh + "]的报告的进度变更为：已完成");
                }
                historyBean.setType(Opt.BGGL_DHYBG_JDGB);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 报告通过审核
     *
     * @param request
     * @param bh
     * @return
     */
    public ResultMsg reportAdoptExamine(HttpServletRequest request, String bh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DSHBG)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }

            if(null == user){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("请登录");
                return ret;
            }

            if(0 == user.getSealId()){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你还没有电子签章");
                return ret;
            }

            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);
            //设报告的进度为待审批
            report.reportProgress = 3;
            //如果是呼吸器 报告
            if(report.type == Report.BG_TYPE_HXQ){
                //设置核验员的id为自己的id
                report.respirator.hyyId = String.valueOf(user.getUserId());
                report.respirator.hyy = user.getUserName();
            }else if(report.type == Report.BG_TYPE_FHQP){  //如果是复合气瓶的报告
                //设置核验员的id为自己的id
                report.cylinders.hyyId = String.valueOf(user.getUserId());
                report.cylinders.hyy = user.getUserName();
            }else if(report.type == Report.BG_TYPE_YLB){  //如果是压力表的报告
                //设置核验员的id为自己的id
                report.pressure.hyyId = String.valueOf(user.getUserId());
                report.pressure.hyy = user.getUserName();
            }
            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            reportDao.save(report);
            ret.setCode(ResultMsg.SUCESS);


            DeleteJobReq deleteJobReq = new DeleteJobReq();
            deleteJobReq.setJobName(Config.PZBG_TASK);
            deleteJobReq.setJobGroupName(Config.PZBG_TASK_GROUP);
            deleteJobReq.setTriggerName(Config.PZBG_TASK);
            deleteJobReq.setTriggerGroupName(Config.PZBG_TASK_GROUP);
            quartzManager.removeJob(deleteJobReq);

            Calendar beforeTime = Calendar.getInstance();
            // 5 秒之后的时间
            beforeTime.add(Calendar.SECOND, Config.MINUTE_5);
            Date beforeDate = beforeTime.getTime();
            AddSimpleJobReq addSimpleJobReq = new AddSimpleJobReq();
            addSimpleJobReq.setDate(beforeDate);
            addSimpleJobReq.setJobClass("PzrwTask");
            quartzManager.addSimpleJob(addSimpleJobReq, Config.PZBG_TASK,Config.PZBG_TASK_GROUP);

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 通过了标识编号为： [" + report.bsbh + "]的待核验报告 ");
                historyBean.setType(Opt.BGGL_DHYBG_TGPZ);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 报告通过 批准
     *
     * @param request
     * @param bh
     * @return
     */
    public ResultMsg reportAdoptApproval(HttpServletRequest request, String bh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            //先查看用户有没有设置签章

            if(0 == user.getSealId()){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你还没有电子签章");
                return ret;
            }

            if(null == user){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("请登录");
                return ret;
            }
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DPZBG)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);

            //如果是呼吸器 报告
            if(report.type == Report.BG_TYPE_HXQ){
                if(!"".equals(user.getUserId())){
                    //设置核验员的id为自己的id
                    report.respirator.pzrId = String.valueOf(user.getUserId());
                    report.respirator.pzr = user.getUserName();
                }

            }else if(report.type == Report.BG_TYPE_FHQP){  //如果是复合气瓶的报告
                if(!"".equals(user.getUserId())){
                    //设置核验员的id为自己的id
                    report.cylinders.pzrId = String.valueOf(user.getUserId());
                    report.cylinders.pzr = user.getUserName();
                }

            }else if(report.type == Report.BG_TYPE_YLB){  //如果是压力表的报告
                if(!"".equals(user.getUserId())){
                    //设置核验员的id为自己的id
                    report.pressure.pzrId = String.valueOf(user.getUserId());
                    report.pressure.pzr = user.getUserName();
                }

            }

            //如果是气瓶或者是面罩  就修改样品信息中的状态
            if(report.type == Report.BG_TYPE_FHQP || report.type == Report.BG_TYPE_HXQ){
                //报告最终批准之后  设置样品的状态
                List<Sample> list =  mSampleDao.getSample(0, report.sbbh, report.bsbh, "");
                if(list.size()>0){
                    Sample  sample22 = list.get(0);
                    switch (report.reportStatue){
                        case 1:   //合格
                            sample22.sampleStatue =4;
                            break;
                        case 2:  // 不合格
                            sample22.sampleStatue =3;
                            break;
                    }
                    //如果是不合格就设置样品里面不合格原因
                    if(sample22.sampleStatue ==3){
                         if(sample22.type == Sample.YP_TYPE_HXQ){
                            switch (report.failedStart){
                                case 1:
                                    sample22.bhgyy=report.respirator.nr_bz;
                                    break;
                                case 2:
                                    sample22.bhgyy=report.respirator.nr_bz;
                                    break;
                                case 3:
                                    sample22.bhgyy="压力表不合格";
                                    break;
                                default:
                                    sample22.bhgyy="";
                                    break;
                            }
                         }else if(sample22.type == Sample.YP_TYPE_FHQP){
                             switch (report.failedStart){
                                 case 1:
                                     sample22.bhgyy="气瓶合格瓶阀不合格";
                                     break;
                                 case 2: //如果是报废就填写报废原因
                                     sample22.bhgyy="报废原因："+report.cylinders.jl;
                                     break;
                                 default:
                                     sample22.bhgyy="";
                                     break;
                             }
                         }
                    }

                    if(sample22.type == Sample.YP_TYPE_HXQ){
                        sample22.respirator.zsjyrq = report.respirator.jyrq;
                    }else{
                        sample22.cylinders.zsjyrq = report.cylinders.jyrq;
                    }
                    mSampleDao.save(sample22);
                }
            }

            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            //设报告的进度为 已完成
            report.reportProgress = 4;
            reportDao.save(report);
            ret.setCode(ResultMsg.SUCESS);

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 通过了标识编号为： [" + report.bsbh + "]的待批准报告 ");
                historyBean.setType(Opt.BGGL_DPZBG_TGPZ);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 驳回报告
     *
     * @param request
     * @return
     */
    public ResultMsg rejectReportDal(HttpServletRequest request, String bh,  int reportProgress, String bhgyy) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DJYBG)
                    && !CheckTool.checkAuthority(user, AuthorityID.BGGL_DPZBG)
                    && !CheckTool.checkAuthority(user, AuthorityID.BGGL_DSHBG)) {

                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            //先查询老的报告
            Report report = reportDao.getReportByBH(bh);
            report.reportProgress = reportProgress;
            //任何情况被驳回都要清除  审核员和 批准员

            //如果是呼吸器 报告
            if(report.type == Report.BG_TYPE_HXQ){
                //设置核验员的id为自己的id
                report.respirator.pzrId = "";
                report.respirator.pzr = "";
                report.respirator.hyyId = "";
                report.respirator.hyy = "";
            }else if(report.type == Report.BG_TYPE_FHQP){  //如果是复合气瓶的报告
                //设置核验员的id为自己的id
                report.cylinders.pzrId = "";
                report.cylinders.pzr = "";
                report.cylinders.hyyId = "";
                report.cylinders.hyy = "";

            }else if(report.type == Report.BG_TYPE_YLB){  //如果是压力表的报告
                //设置核验员的id为自己的id
                report.pressure.pzrId = "";
                report.pressure.pzr = "";
                report.pressure.hyyId = "";
                report.pressure.hyy = "";
            }
            //不合格原因
            report.bhgyy = bhgyy;
            long time = TimeUtil.getCurrentTimeMillis();
            report.setUpdateUser(user.getUserId() + "");
            report.setUpdateTime(time);
            reportDao.save(report);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("成功");

            //
            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 驳回了标识编号为： [" + report.bsbh + "]的报告 ");
                historyBean.setType(Opt.BGGL_BHBG);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 修改报告内容
     * @param request
     * @param objectJson
     * @return
     */
    public ResultMsg updateReport(HttpServletRequest request, String objectJson) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DJYBG)
                    && !CheckTool.checkAuthority(user, AuthorityID.BGGL_DPZBG)
                    && !CheckTool.checkAuthority(user, AuthorityID.BGGL_DSHBG)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            Report report = JSON.parseObject(objectJson, Report.class);
            report.setUpdateTime(TimeUtil.getCurrentTimeMillis());
            report.setUpdateUser(user.getUserId() + "");
            reportDao.save(report);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("修改成功");

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 修改了标识编号为： [" + report.bsbh + "]的报告信息 ");
                historyBean.setType(Opt.BGGL_XGBGNR);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(report.bsbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 查询 检验时间即将到期的 报告
     * @param request
     * @param wtfId
     * @param month
     * @return
     */
    public ResultMsg  getReportOverdue(HttpServletRequest request,String wtfId,int month,String wtfIdChi ){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            long tadays = TimeUtil.getCurrentDayZero();
            Date date = new Date();
            date.setTime(tadays);
            //计算N天后的时间毫秒值
            long  calculation =  TimeUtil.getDateAfter(date,month*30);
            List<Report> reports = reportDao.getReportOverdue(wtfId,calculation,wtfIdChi);

            ret.setCode(ResultMsg.SUCESS);
            ret.setData(reports);
            ret.setMsg("查询成功");

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 删除报告
     * @return
     */
    public ResultMsg deleteBg(HttpServletRequest request,String bh,String bsbh){
        ResultMsg ret = new ResultMsg();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);


            if("".equals(bh)){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
           long count =  reportDao.deleteByBh(bh);
            //报告删除了之后 要去吧样品里面的  不合格原因，样品状态都从新改一下
            Sample  xsample =  mSampleDao.getSampleById(bsbh);
            if(null != xsample){
                xsample.sampleStatue = 2;
                xsample.bhgyy ="";
                mSampleDao.save(xsample);
            }

            if(count>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("删除成功");
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 删除了标识编号为： [" + bsbh + "] 的报告");
                historyBean.setType(Opt.BGGL_SCBG);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(bsbh);
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
                return ret;
            }



        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     *
     * 直接生成 气瓶报废的报告
     * @param request
     * @param bsbh
     * @return
     */
    public ResultMsg setBfReport(HttpServletRequest request,String bsbh,String jl){
        ResultMsg ret = new ResultMsg();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            if("".equals(bsbh) || "".equals(jl)){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            //查询 样品信息
            Sample sample =   mSampleDao.getSampleById(bsbh);
            if(null != sample){
                //就生成一个气瓶报告
                Report report = new Report();
                report.bh = sample.bh;
                //标识编号
                report.bsbh = sample.bsbh;
                //任务单编号
                report.rwdbh = sample.rwdbh;
                //设备编号
                report.sbbh = sample.sbbh;
                //样品类型
                report.type = report.BG_TYPE_FHQP;
                report.spType = sample.spType;
                //设置短信的发送状态
                report.isSendSMS = 0;
                //接收日期时间戳
                report.jsrqLong = sample.jsrqLong;

                report.pxzd = sample.pxzd;

                //气瓶的状态
                report.reportStatue = 2;//不合格

                //报告的进展  待核验
                report.reportProgress = 1;

                report.zfzddhy = 0;
                //子类状态 直接设置报废
                report.failedStart = 2;
                report.bsbhHrader = sample.bsbhHrader;
                report.erweima = sample.erweima;
                report.wtfId = sample.cylinders.wtfId;
                report.wtf = sample.cylinders.wtf;
                report.wtfIdChi = sample.wtfIdChi;
                report.wtfChi = sample.wtfChi;
                report.sjr = sample.sjr;
                report.zzcsId = sample.zzcsId;
                report.zzcs = sample.zzcs;
                report.jyyId = sample.cylinders.jcrId;
                //设置气瓶的数据
                CylindersReport cylindersReport = new CylindersReport();
                cylindersReport.bh = sample.bh;
                cylindersReport.wtfId = sample.cylinders.wtfId;
                cylindersReport.wtf = sample.cylinders.wtf;
                //是否充气
                cylindersReport.sfcq =sample.cylinders.sfcq;
                //送检日期
                cylindersReport.sjrq = sample.cylinders.sjri;


                //共送检气瓶数
                cylindersReport.gsjqps = 1;
                // 结论
                cylindersReport.jl = jl;
                cylindersReport.cljg = "报废";
                //合格气瓶数/报废气瓶数
                cylindersReport.hgqps = 1;
                List<String> bb = new ArrayList<>();
                bb.add(sample.sbbh);
                cylindersReport.bhlist = bb;
                cylindersReport.sjr = sample.cylinders.sjr;
                cylindersReport.sjrId = sample.cylinders.sjrId;
                cylindersReport.jyyId = sample.cylinders.jcrId;
                cylindersReport.jyy = sample.cylinders.jcr;
                cylindersReport.gcsrj = sample.cylinders.qpsrj;

                //计算有效期
                // 先获取到 送检日期    3年后的前一天
                long  nowTime =TimeUtil.currentTimeMillis();
                report.jyrqNian = TimeUtil.formatTime_Year(nowTime);
                report.jyrqYue = TimeUtil.formatTime_Month(nowTime);
                cylindersReport.jyrq = TimeUtil.formatTime_tow(nowTime);
                Date date2 = new Date(nowTime);
                long year3 = TimeUtil.getDateBeforeYear(date2, 3);

                String mc_scrq = "";
                if(sample.cylinders.scrq.length() == 7){
                    mc_scrq =sample.cylinders.scrq+".01";
                }else{
                    mc_scrq = sample.cylinders.scrq;
                }
                //得到15生产日期 15年后
                long year15 = TimeUtil.getDateBeforeYear(TimeUtil.parsex(mc_scrq), 15);

                if (year3 > year15) {
                    cylindersReport.yxrq = TimeUtil.formatTime_tow(year15);
                    report.xcjyrq  = year15;
                } else {
                    cylindersReport.yxrq = TimeUtil.formatTime_tow(year3);
                    report.xcjyrq  = year3;
                }
                report.setAddTime(TimeUtil.currentTimeMillis());
                cylindersReport.jydw = "";
                OtherInput mOtherInput = otherInputDao.getOtherInput();
                if(null!=mOtherInput){
                    cylindersReport.jydwsqh =mOtherInput.qp_jydwsqh;
                }else{
                    //检验单位授权号 写死
                    cylindersReport.jydwsqh = Config.QP_JTDWSQH;
                }
                //查询样品信息的最警的样品
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("成功");
                cylindersReport.isOldJiqi = 1;
                cylindersReport.hltUrl ="";
                //设置报告测试时间
                cylindersReport.bgCsrq =nowTime;
                report.cylinders = cylindersReport;
                reportDao.save(report);
            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }






//    /**
//     * 保存图片到本地
//     * @param data
//     * @param fileName
//     */
//    public boolean  saveImageToLoacl(byte [] data, String fileName)  {
//        boolean isok=false;
//        try {
//            File mfile = new File(Config.UPLOADFOLDER+"\\fxq");
//            if(!mfile .exists()  && !mfile .isDirectory()){
//                mfile .mkdir();
//            }
//            if(null !=data && data.length>20 ){
//                ByteArrayInputStream bais = new ByteArrayInputStream(data);
//                BufferedImage bi1 = ImageIO.read(bais);
//                File w2 = new File(mfile.getAbsolutePath()+"\\"+fileName);//可以是jpg,png,gif格式
//                ImageIO.write(bi1, "png", w2);//不管输出什么格式图片，此处不需改动
//            }
//
//            //上成功之后 判断是本地服务器还是远程服务器
//            if(Config.isLocahost){ //如果是本地服务器
//                String  resourceId  = "";
//                File mmfile = new File(Config.UPLOADFOLDER+"\\fxq"+"/"+fileName);
//                if (mmfile.exists()) {
//                    FileItem fileItem = FileUtils.getMultipartFile(mmfile,"templFileItem");
//                    MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
//                    //获取文件的文件名字(后面要用到)
//                    String  filename  =  multipartFile.getOriginalFilename();
//                    //这个url是要上传到另一个服务器上接口
//                    String  url  =  String.format(Config.DISTANCE_UPLOAD_FILE);
//                    Object  object  =  null;
//                    //创建HttpClients实体类
//                    CloseableHttpClient aDefault  =  HttpClients.createDefault();
//                    try {
//                        HttpPost httpPost = new HttpPost(url);
//                        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//                        //这里就是自己踩的坑,需要使用file.getBytes()
//                        //builder.addBinaryBody("file",file.getInputStream(),ContentType.create("multipart/form-data"),filename);
//                        //使用这个，另一个服务就可以接收到这个file文件了
//                        builder.addBinaryBody("file", multipartFile.getBytes(), ContentType.create("multipart/form-data"), filename);
//                        StringBody stringBody = new StringBody(resourceId, ContentType.MULTIPART_FORM_DATA);
//                        // builder.addPart("resource_id", stringBody);
//                        HttpEntity entity = builder.build();
//                        httpPost.setEntity(entity);
//                        ResponseHandler<Object> rh = new ResponseHandler<Object>() {
//                            @Override
//                            public Object handleResponse(HttpResponse response) throws IOException {
//                                HttpEntity entity = response.getEntity();
//                                String result = EntityUtils.toString(entity, "UTF-8");
//                                return result;
//                            }
//                        };
//                        aDefault = HttpClients.createDefault();
//                        object = aDefault.execute(httpPost, rh);
//                    }catch(Exception e){
//                        System.out.println(e.getMessage());
//                    }
//                }
//            }else{ //远程服务器
//                //将上传的文件保存到要下载的数据库
//                DownFileBean downFileBean = new DownFileBean();
//                downFileBean.setFileUrl("/fxq/"+fileName);
//                int mres = downFileMapper.addDownFile(downFileBean);
//                if(mres>0){
//                    isok= true;
//                }else{
//                    isok =false;
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            isok = false;
//        }
//        return  isok;
//    }

    /**
     * 保存图片到本地
     * @param data
     * @param fileName
     */
    public boolean  saveImageToLoacl(byte [] data,String fileName)  {
        try {
            File mfile = new File(Config.UPLOADFOLDER+"\\fxq");
            if(!mfile .exists()  && !mfile .isDirectory()){
                mfile .mkdir();
            }
            if(null !=data && data.length>20 ){
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                BufferedImage bi1 = ImageIO.read(bais);
                File w2 = new File(mfile.getAbsolutePath()+"\\"+fileName);//可以是jpg,png,gif格式
                ImageIO.write(bi1, "png", w2);//不管输出什么格式图片，此处不需改动
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 保存图片到本地
     * @param data
     * @param fileName
     */
    public boolean  saveImageToLoacl2(byte [] data,String fileName) {
        try {
            File mfile = new File(Config.UPLOADFOLDER2);
            if(!mfile .exists()  && !mfile .isDirectory()){
                mfile .mkdir();
            }
            if(null !=data && data.length>20 ){
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                BufferedImage bi1 = null;

                bi1 = ImageIO.read(bais);
                File w2 = new File(Config.UPLOADFOLDER2+"/"+fileName);//可以是jpg,png,gif格式
                ImageIO.write(bi1, "png", w2);//不管输出什么格式图片，此处不需改动
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


//    /**
//     * 保存图片到本地
//     * @param data
//     * @param fileName
//     */
//    public boolean  saveImageToLoacl2(byte [] data,String fileName) {
//        try {
//            File mfile = new File(Config.UPLOADFOLDER+"\\fhqp");
//            if(!mfile .exists()  && !mfile .isDirectory()){
//                mfile .mkdir();
//            }
//            if(null !=data && data.length>20 ){
//                ByteArrayInputStream bais = new ByteArrayInputStream(data);
//                BufferedImage bi1 = null;
//
//                    bi1 = ImageIO.read(bais);
//                File w2 = new File(mfile.getAbsolutePath()+"\\"+fileName);//可以是jpg,png,gif格式
//                ImageIO.write(bi1, "png", w2);//不管输出什么格式图片，此处不需改动
//            }
//
//            //上成功之后 判断是本地服务器还是远程服务器
//            if(Config.isLocahost) { //如果是本地服务器
//                String  resourceId  = "";
//                File mmfile = new File(Config.UPLOADFOLDER+"\\fhqp"+"/"+fileName);
//                if (mmfile.exists()) {
//                    FileItem fileItem = FileUtils.getMultipartFile(mmfile,"templFileItem");
//                    MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
//                    //获取文件的文件名字(后面要用到)
//                    String  filename  =  multipartFile.getOriginalFilename();
//                    //这个url是要上传到另一个服务器上接口
//                    String  url  =  String.format(Config.DISTANCE_UPLOAD_FILE_REPORT);
//                    Object  object  =  null;
//                    //创建HttpClients实体类
//                    CloseableHttpClient aDefault  =  HttpClients.createDefault();
//                    try {
//                        HttpPost httpPost = new HttpPost(url);
//                        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//                        //这里就是自己踩的坑,需要使用file.getBytes()
//                        //builder.addBinaryBody("file",file.getInputStream(),ContentType.create("multipart/form-data"),filename);
//                        //使用这个，另一个服务就可以接收到这个file文件了
//                        builder.addBinaryBody("file", multipartFile.getBytes(), ContentType.create("multipart/form-data"), filename);
//                        StringBody stringBody = new StringBody(resourceId, ContentType.MULTIPART_FORM_DATA);
//                        // builder.addPart("resource_id", stringBody);
//                        HttpEntity entity = builder.build();
//                        httpPost.setEntity(entity);
//                        ResponseHandler<Object> rh = new ResponseHandler<Object>() {
//                            @Override
//                            public Object handleResponse(HttpResponse response) throws IOException {
//                                HttpEntity entity = response.getEntity();
//                                String result = EntityUtils.toString(entity, "UTF-8");
//                                return result;
//                            }
//                        };
//                        aDefault = HttpClients.createDefault();
//                        object = aDefault.execute(httpPost, rh);
//                    }catch(Exception e){
//                        System.out.println(e.getMessage());
//                    }
//                }
//            }else{
//                //将上传的文件保存到要下载的数据库
//                DownFileBean downFileBean = new DownFileBean();
//                downFileBean.setFileUrl("/fhqp/"+fileName);
//                int mres = downFileMapper.addDownFile(downFileBean);
//            }
//            return true;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        }
//    }


//    /**
//     * 保存doc到本都
//     * @param oldQiPingBean
//     * @param mimgArr
//     * @param fileName
//     * @param bsbh
//     * @return
//     */
//    public boolean  saveDocToLoacl2(OldQiPingBean oldQiPingBean,byte [] mimgArr,String fileName,String bsbh) {
//        try {
//
//            oldQiPingBean.setTup(mimgArr);
//            BASE64Decoder decoder = new BASE64Decoder();
//            OutputStream os = new FileOutputStream(Config.UPLOADFOLDER+"\\fhqp\\"+fileName);
//            os.write(mimgArr, 0, mimgArr.length);
//            os.flush();
//            os.close();
//
//            //上成功之后 判断是本地服务器还是远程服务器
//            if(Config.isLocahost) { //如果是本地服务器
//                String  resourceId  = "";
//                File mmfile = new File(Config.UPLOADFOLDER+"\\fhqp"+"/"+fileName);
//                if (mmfile.exists()) {
//                    FileItem fileItem = FileUtils.getMultipartFile(mmfile,"templFileItem");
//                    MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
//                    //获取文件的文件名字(后面要用到)
//                    String  filename  =  multipartFile.getOriginalFilename();
//                    //这个url是要上传到另一个服务器上接口
//                    String  url  =  String.format(Config.DISTANCE_UPLOAD_FILE_REPORT);
//                    Object  object  =  null;
//                    //创建HttpClients实体类
//                    CloseableHttpClient aDefault  =  HttpClients.createDefault();
//                    try {
//                        HttpPost httpPost = new HttpPost(url);
//                        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//                        //这里就是自己踩的坑,需要使用file.getBytes()
//                        //builder.addBinaryBody("file",file.getInputStream(),ContentType.create("multipart/form-data"),filename);
//                        //使用这个，另一个服务就可以接收到这个file文件了
//                        builder.addBinaryBody("file", multipartFile.getBytes(), ContentType.create("multipart/form-data"), filename);
//                        StringBody stringBody = new StringBody(resourceId, ContentType.MULTIPART_FORM_DATA);
//                        // builder.addPart("resource_id", stringBody);
//                        HttpEntity entity = builder.build();
//                        httpPost.setEntity(entity);
//                        ResponseHandler<Object> rh = new ResponseHandler<Object>() {
//                            @Override
//                            public Object handleResponse(HttpResponse response) throws IOException {
//                                HttpEntity entity = response.getEntity();
//                                String result = EntityUtils.toString(entity, "UTF-8");
//                                return result;
//                            }
//                        };
//                        aDefault = HttpClients.createDefault();
//                        object = aDefault.execute(httpPost, rh);
//                    }catch(Exception e){
//                        System.out.println(e.getMessage());
//                    }
//                }
//            }else{
//                //将上传的文件保存到要下载的数据库
//                DownFileBean downFileBean = new DownFileBean();
//                downFileBean.setFileUrl("/fhqp/"+fileName);
//                int mres = downFileMapper.addDownFile(downFileBean);
//            }
//            return true;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        }
//    }


}
